import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import {  HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import {Service} from '../../../server/server';
import {InitService} from '../../../public/serve/init/init.service';
import { pushedAddType } from '../pushed.data'
import {DatePipe} from '@angular/common';
import {CudTableService} from '../../../public/serve/cud-table/cud-table.service';

declare var $: any;

interface pushedAddResponse {
    result: string[];
    result_code: string;
    message: string;
}


@Component({
  selector: 'app-pushed-add',
  templateUrl: './pushed-add.component.html',
})

export class PushedAddComponent implements OnInit {
  constructor(private http: HttpClient, private router: Router, private server:Service,public initService: InitService, private datePipe: DatePipe, private tableListService: CudTableService) {
  }

  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();
  //清空
  @ViewChild('addForm') formElement: HTMLFormElement;
  public parentList : any;
  reset() {
    $("#add").modal("hide");
    this.formElement.reset();
    setTimeout(() => {
      this.default();
    }, 300);
  }

  //默认数据
  default() {
    this.pushedAdd.themeGroupId = 0;
  }

  //datatype
  pushedAdd: pushedAddType = {
    parentId: '',
    child: 0,
    name: '',
    status: 0,
    create_by :  '',
    createTime: '',
    description: '',
    themeGroupId: 0,
    themeTableId:''
  };

  //用户id
  createUserId: any;

  ngOnInit() {

  }

    //add
    add(v): void {
        let body = {
            child: Number(this.pushedAdd.child),
            createBy: this.server.getuserName(),
            themeTableId: '',
            createTime: this.datePipe.transform(new Date(), 'yyyy-MM-dd HH:mm:ss').toString(),
            description: this.pushedAdd.description,
            name: this.pushedAdd.name,
            parentId: Number(this.pushedAdd.parentId),
            status: Number(this.pushedAdd.status),
            themeGroupId: Number(this.pushedAdd.themeGroupId)
        };

    this.http.post<pushedAddResponse>(this.server.baseUrl + 'Service/ThemeWords/addThemeTable', body)
      .subscribe(res => {
        this.addOutput.emit(res.result_code);
        //console.log(res)
        if (res.result_code === '0000') {
          this.reset();
          this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message };
          this.server.promptclose;
        } else {
          this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message };
          this.server.promptclose;
        }
      });
  }

}
